﻿using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;

namespace TiaoTiaoCode.AutoUpdate.ServerUI.Splashs
{
    public class LoadingDialogViewModel : BindableBase, IDialogAware
    {
        readonly IEventAggregator _eventAggr;

        private string loadingMsg = default!;

        /// <summary>
        /// loading文本
        /// </summary>
        public string LoadingMsg
        {
            get { return loadingMsg; }
            set { SetProperty(ref loadingMsg, value); }
        }

        public LoadingDialogViewModel(IEventAggregator eventAggregator)
        {
            _eventAggr = eventAggregator;

            _eventAggr.GetEvent<LoadingDialogPubSubEvent>().Subscribe(LoadingCommand);
        }

        /// <summary>
        /// Splash命令事件订阅响应
        /// </summary>
        /// <param name="msg"></param>
        void LoadingCommand(LoadingDialogPubSubEventMessage msg)
        {
            //if (msg.Command == SplashCommand.Show)
            //    LoadingMsg = msg.LoadingContent;
            //else
            //{
            //    _eventAggr.GetEvent<LoadingDialogPubSubEvent>().Unsubscribe(LoadingCommand);
            //    RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
            //}

            if (msg.Command == SplashCommand.Hide)
            {
                _eventAggr.GetEvent<LoadingDialogPubSubEvent>().Unsubscribe(LoadingCommand);
                RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
            }
            else if (msg.Command == SplashCommand.ReWriteContent)
                LoadingMsg = msg.LoadingContent;
        }

        #region Dialog接口

        public string Title => String.Empty;

        public event Action<IDialogResult>? RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            var content = parameters.GetValue<string>("LoadingContent");

            LoadingMsg = content;
        }

        #endregion
    }
}
